package com.ymatou.iapi.pushcallback.testcase;

import com.ymatou.iapi.pushcallback.parameter.pushcallbackBean;
import com.ymatou.iapi.pushcallback.service.pushcallbackCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import org.junit.*;
import org.junit.experimental.categories.Category;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
*个推消息回执
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_pushcallback{
	private static pushcallbackBean pushcallbackBean;
	private static pushcallbackCall pushcallbackCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("个推消息回执");
	}

	@Before
	public void caseUp() {
		pushcallbackBean=new pushcallbackBean();
		pushcallbackCall=new pushcallbackCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(pushcallbackCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_pushcallback_001() {
		Logger.start(true,"个推消息回执");
		try {
			String masterSecret = "pHVHgXt3Ro9ckoO9hKiyoA";
			String appId = "kc17rENMp79sKXqBqoRSh5";
			pushcallbackBean.setAppid(appId);
			String cId = "4147c8ddf5f6169110710d71b7b06e0f";
			pushcallbackBean.setCid(cId);
			String taskid = "MMPS-1018-55ac2c0c-ad3e-48f5-95b7-ccfbc852c854";
			pushcallbackBean.setTaskid(taskid);
			pushcallbackBean.setActionId("actionId");
			pushcallbackBean.setCode("200");
			pushcallbackBean.setDesc("hello,world");
			String msgId = "MMPS-1018-7902d9f5-3f90-491c-be25-9c421b8439b7";
			pushcallbackBean.setMsgid(msgId);
			pushcallbackBean.setSign(string2MD5(appId + cId + taskid + msgId + masterSecret));
			pushcallbackCall.setData(pushcallbackBean);
			pushcallbackCall.callService();			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	public static String string2MD5(String inStr) {
		MessageDigest md5 = null;
		try {
			md5 = MessageDigest.getInstance("MD5");
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		char[] charArray = inStr.toCharArray();
		byte[] byteArray = new byte[charArray.length];

		for (int i = 0; i < charArray.length; i++)
			byteArray[i] = (byte) charArray[i];
		byte[] md5Bytes = md5.digest(byteArray);
		StringBuilder hexValue = new StringBuilder();
		for (int i = 0; i < md5Bytes.length; i++) {
			int val = ((int) md5Bytes[i]) & 0xff;
			if (val < 16)
				hexValue.append("0");
			hexValue.append(Integer.toHexString(val));
		}
		return hexValue.toString();

	}

}